using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Telerik.Web.UI;

namespace WebForms.Demo_2
{
    public partial class RadTreeView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TelerikRadTreeView.DataSource = GetData();
                TelerikRadTreeView.DataBind();
            }
        }
        private DataTable GetData()
        {
            SqlCommand selectCommand = new SqlCommand("SELECT * FROM Employees");
            selectCommand.Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindCOnnectionString"].ConnectionString);
            SqlDataAdapter adapter = new SqlDataAdapter(selectCommand);

            DataTable data = new DataTable();
            adapter.Fill(data);

            return data;
        }

        protected void TelerikRadTreeView_NodeDrop(object sender, Telerik.Web.UI.RadTreeNodeDragDropEventArgs e)
       {
           RadTreeNode sourceNode = e.SourceDragNode;
           RadTreeNode destNode = e.DestDragNode;
           RadTreeViewDropPosition dropPosition = e.DropPosition;
           if (destNode != null)
           {
               if (sourceNode.TreeView.SelectedNodes.Count <= 1)
               {
                   PerformDragAndDrop(dropPosition, sourceNode, destNode);
               }
               else if (sourceNode.TreeView.SelectedNodes.Count > 1)
               {
                   foreach (RadTreeNode node in sourceNode.TreeView.SelectedNodes)
                   {
                       PerformDragAndDrop(dropPosition, node, destNode);
                   }
               }
               destNode.Expanded = true;
               sourceNode.TreeView.ClearSelectedNodes();
           }
       }
       private static void PerformDragAndDrop(RadTreeViewDropPosition dropPosition, RadTreeNode sourceNode, RadTreeNode destNode)
       {
           switch (dropPosition)
           {
               case RadTreeViewDropPosition.Over:
                   // child
                   if (!sourceNode.IsAncestorOf(destNode))
                   {
                       sourceNode.Owner.Nodes.Remove(sourceNode);
                       destNode.Nodes.Add(sourceNode);
                   }
                   break;
               case RadTreeViewDropPosition.Above:
                   // sibling - above
                   sourceNode.Owner.Nodes.Remove(sourceNode);
                   destNode.InsertBefore(sourceNode);
                   break;
               case RadTreeViewDropPosition.Below:
                   // sibling - below
                   sourceNode.Owner.Nodes.Remove(sourceNode);
                   destNode.InsertAfter(sourceNode);
                   break;
           }
       }
   }
}